<?php
/**
 * Author: Sam
 * DateTime: 2019-04-11 11:17
 * Description:
 */

namespace Acme\TestBundle\Repository\Traits;

/**
 * Author: Sam
 * DateTime: 2019-04-22 16:19
 * Trait CommonTrait
 * Description:repository 公共方法
 * @package AppBundle\Repository\Traits
 */
Trait CommonTrait
{
    /**
     * Author: Sam
     * DateTime: 2019-04-09 12:53
     * Description:setter方法
     * @param $key
     * @return string
     */
    public function setterFunction($key)
    {
        return 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
    }

    /**
     * Author: Sam
     * DateTime: 2019-04-15 09:45
     * Description:getter方法
     * @param $key
     * @return string
     */
    public function getterFunction($key)
    {
        return 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
    }

    /**
     * Author: Sam
     * DateTime: 2019-04-15 09:48
     * Description:更新实体
     * @param $entity
     * @return mixed
     */
    public function save($entity)
    {
        $this->_em->persist($entity);
        return $this->_em->flush();
    }

    /**
     * Author: Sam
     * DateTime: 2019-05-07 10:40
     * Description:删除实体
     * @param $entity
     * @return mixed
     */
    public function delete($entity)
    {
        $this->_em->remove($entity);
        return $this->_em->flush();
    }

    /**
     * Author: Sam
     * DateTime: 2019-04-15 10:58
     * Description:获取仓库
     * @param $entityName
     * @return mixed
     */
    public function getRepository($entityName)
    {
        return $this->_em->getRepository($entityName);
    }

    /**
     * Author: Sam
     * DateTime: 2019-04-15 12:53
     * Description:获取单条数据
     * @param $sql
     * @return mixed
     */
    public function fetch($sql)
    {
        $res = $this->_em->getConnection()
            ->prepare($sql);
        $res->execute();

        return $res->fetch();
    }

    /**
     * Author: Sam
     * DateTime: 2019-04-19 15:32
     * Description:获取多条数据
     * @param $sql
     * @return mixed
     */
    public function fetchAll($sql)
    {
        $res = $this->_em->getConnection()
            ->prepare($sql);
        $res->execute();

        return $res->fetchAll();
    }

    /**
     * Author: Sam
     * DateTime: 2019-04-15 13:25
     * Description:执行sql
     * @param $sql
     * @return mixed
     */
    public function execute($sql)
    {
        $res = $this->_em->getConnection()
            ->prepare($sql);
        return $res->execute();
    }

    /**
     * Author: Sam
     * DateTime: 2019-04-15 13:26
     * Description:开启事务
     * @return mixed
     */
    public function beginTransaction()
    {
        return $this->_em->getConnection()->beginTransaction();
    }

    /**
     * Author: Sam
     * DateTime: 2019-04-15 13:26
     * Description:提交事务
     * @return mixed
     */
    public function commit()
    {
        return $this->_em->getConnection()->commit();
    }

    /**
     * Author: Sam
     * DateTime: 2019-04-15 13:27
     * Description:回滚事务
     * @return mixed
     */
    public function rollback()
    {
        return $this->_em->getConnection()->rollback();
    }

    /**
     * Author: Sam
     * DateTime: 2019-04-15 16:16
     * Description:自增
     * @param $entity
     * @param $field
     * @param $amount
     * @return mixed
     */
    public function increment($entity,$field,$amount = 1)
    {
        $setFunction = $this->setterFunction($field);
        $getFunction = $this->getterFunction($field);
        $entity->$setFunction(bcadd($entity->$getFunction(),$amount));
        return $this->save($entity);
    }

    /**
     * Author: Sam
     * DateTime: 2019-04-15 16:16
     * Description:自减
     * @param $entity
     * @param $field
     * @param $amount
     * @return mixed
     */
    public function decrement($entity,$field,$amount = 1)
    {
        $setFunction = $this->setterFunction($field);
        $getFunction = $this->getterFunction($field);
        $entity->$setFunction(bcsub($entity->$getFunction(),$amount));
        return $this->save($entity);
    }
}